package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
@Service
public class ShoppingCartServiceimpl implements ShoppingCartService {


    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public void addCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart cart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, cart);
        cart.setUserId(BaseContext.getCurrentId());
        List<ShoppingCart> list = shoppingCartMapper.getCart(cart);

        if (list != null && list.size() > 0) {
            list.get(0).setNumber(list.get(0).getNumber()+1);
            shoppingCartMapper.updateCartNumber(list.get(0));
        } else {
            if (shoppingCartDTO.getDishId() != null) {
                Dish dish = dishMapper.getDish(shoppingCartDTO.getDishId().intValue());
                ShoppingCart shoppingCart = ShoppingCart.builder()
                        .dishId(shoppingCartDTO.getDishId())
                        .image(dish.getImage())
                        .name(dish.getName())
                        .number(1)
                        .userId(BaseContext.getCurrentId())
                        .createTime(LocalDateTime.now())
                        .dishFlavor(shoppingCartDTO.getDishFlavor())
                        .amount(dish.getPrice())
                        .build();
                shoppingCartMapper.addCart(shoppingCart);
            } else {
                Setmeal setmeal = setmealMapper.getSetmeal(shoppingCartDTO.getSetmealId().intValue());
                ShoppingCart shoppingCart = ShoppingCart.builder()
                        .dishId(shoppingCartDTO.getDishId())
                        .image(setmeal.getImage())
                        .name(setmeal.getName())
                        .number(1)
                        .userId(BaseContext.getCurrentId())
                        .createTime(LocalDateTime.now())
                        .dishFlavor(shoppingCartDTO.getDishFlavor())
                        .amount(setmeal.getPrice())
                        .build();
                shoppingCartMapper.addCart(shoppingCart);
            }
        }


    }

    @Override
    public List<ShoppingCart> getCartList() {
        Long currentId = BaseContext.getCurrentId();
        List<ShoppingCart> list=  shoppingCartMapper.getCartList(currentId);
        return list;
    }

    @Override
    public void cleanCart(Long currentId) {
        shoppingCartMapper.cleanCart(currentId);
    }
}
